// qstata 02_pull_var_names_labels.do --memsize=16
**Objective: Pull in all raw ECs for every year and append/save separately 

*Set directories 
/* REDACTED 
raw_data_dir is redacted (an empty string is created instead) to avoid disclosing location of raw files*/
local raw_data_dir = ""
local import_dir = "/projects/data_p/import/"
local build_raw_dir = "/projects/data_p/build/raw/"
/* REDACTED
var2 and var4 below have been renamed from original code files*/
local ec_dir = "`1'"
local var2 = "`2'"
local year_order = "`3'"
local var4 = "`4'"
*First output list of vars and labels across all years to inspect 

cd "`raw_data_dir'`ec_dir'/"
local filelist: dir . dirs "*"
di `filelist'
foreach p of local filelist {
	if "`p'" != "doc"{
		cd "`p'"
		if "`p'" == "2017" {
			/* REDACTED 
			Code importing the raw file is redacted to avoid disclosing name of raw file.
			Comment discussing structure of raw file is redacted */
			descsave, saving("`import_dir'EC/`ec_dir'/varlist/varlist_`p'.dta", replace) gsort(name) keep(name varlab)
			use "`import_dir'EC/`ec_dir'/varlist/varlist_`p'.dta", clear
			gen year_`p' = 1
			save, replace 
		}
		else if "`p'" == "2012"{			
			/* REDACTED 
			Code importing the raw file is redacted to avoid disclosing name of raw file.
			*/
			descsave, saving("`import_dir'EC/`ec_dir'/varlist/varlist_`p'.dta", replace) gsort(name) keep(name varlab)
			use "`import_dir'EC/`ec_dir'/varlist/varlist_`p'.dta", clear
			gen year_`p' = 1
			save, replace 
		}
		else {
			/* REDACTED 
			Code importing the raw file is redacted to avoid disclosing name of raw file.
			*/
			descsave, saving("`import_dir'EC/`ec_dir'/varlist/varlist_`p'.dta", replace) gsort(name) keep(name varlab)
			use "`import_dir'EC/`ec_dir'/varlist/varlist_`p'.dta", clear
			gen year_`p' = 1
			save, replace
		}
	}
	cd "`raw_data_dir'`ec_dir'/"
}

clear
cd "`import_dir'EC/`ec_dir'/varlist/"
local filelist: dir . files "*.dta"
append using `filelist'
save "`import_dir'EC/`ec_dir'/all_vars.dta", replace
collapse (mean) year*, by(name varlab)
sort name varlab
order name varlab `year_order'
export excel using "`import_dir'EC/`ec_dir'/all_vars.xlsx", firstrow(variables) replace
